package com.pb.infra.auth.controller;

import com.pb.infra.auth.entity.byd.Index;
import com.pb.infra.auth.entity.pbsys.FileDel;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.service.CommonService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 通用模块
 *
 * @author:liudebao
 * @date: 2019/7/29
 */
@Slf4j
@RestController
@RequestMapping("v1/sys/common")
public class CommonController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private CommonService commonService;

    /**
     * 读取标准代码
     * @param type 标准代码类型
     * @return Result 返回数据
     */
    @GetMapping("selBusiCode")
    public Result selBusiCode(@RequestParam String type) {
        return commonService.selBusiCode(type);
    }

    /**
     * 读取数据库类型信息
     * @return Result 返回数据
     */
    @GetMapping("selectBaseType")
    public Result selectBaseType() {
        return commonService.selectBaseType();
    }

    /**
     * 查询码值有效标识
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("selectCodeTypeList")
    public Result selectCodeTypeList(@RequestBody Map<String,Object> map) {
        return Result.ok(commonService.selectCodeTypeList(map));
    }

    /**
     * 查询数据存放类型
     * @return Result 返回数据
     */
    @GetMapping("selectStoreTypeList")
    public Result selectStoreTypeList() {
        return commonService.selectStoreTypeList();
    }

    /**
     * 查询数据源类型
     * @return Result 返回数据
     */
    @GetMapping("selSourceType")
    public Result selSourceType() {
        return commonService.selSourceType();
    }

    /**
     * 查询数据源用途
     * @return Result 返回数据
     */
    @GetMapping("selSourceUse")
    public Result selSourceUse() {
        return commonService.selSourceUse();
    }

    /**
     * 查询历史数据类型
     * @return Result 返回数据
     */
    @GetMapping("selHisData")
    public Result selHisData() {
        return commonService.selHisData();
    }

    /**
     * 查询用户所属法人信息
     * @return Result 返回数据
     */
    @PostMapping("selCorpList")
    public Result selCorpList(HttpServletRequest request) {
        return commonService.selCorpList(request);
    }

    /**
     * 查询多法人用户法人+管理法人列表
     * @return Result 返回数据
     */
    @PostMapping("selDisCorp")
    public Result selDisCorp(HttpServletRequest request) {
        return commonService.selDisCorp(request);
    }

    /**
     * 查询用户登录职位所属法人+管理法人
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("selRuleCorpList")
    public Result selRuleCorpList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        return commonService.selRuleCorpList(map, request);
    }

    /**
     * 查询用户职位
     * @return Result 返回数据
     */
    @PostMapping("selUserPosition")
    public Result selUserPosition(HttpServletRequest request) {
        return commonService.selUserPosition(request);
    }

    /**
     * 查询用户主职位
     * @return Result 返回数据
     */
    @PostMapping("selMainPositon")
    public Result selMainPositon(HttpServletRequest request) {
        return commonService.selMainPositon(request);
    }

    /**
     * 查询职位信息
     * @param map 请求参数
     * @return Result 返回数据
     */
    @GetMapping("selPositionInfo")
    public Result selPositionInfo(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        return commonService.selPositionInfo(map, request);
    }

    /**
     * 查询按钮权限
     * @param map
     * @param request
     * @return
     */
    @GetMapping("selBtnAuthInfo")
    public Result selBtnAuthInfo(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        return commonService.selBtnAuthInfo(map, request);
    }

    /**
     * 查询所有法人
     * @return Result 返回数据
     */
    @GetMapping("selAllCorpList")
    public Result selAllCorpList() {
        return commonService.selAllCorpList();
    }

    /**
     * 查询法人所属机构
     * @param map 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getDimOrgTree")
    public Result getDimOrgTree(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        return Result.ok(commonService.getDimOrgTree(map, request));
    }

    /**
     * 查询组织架构树过滤部门
     * @param map 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getOrgStTree")
    public Result getOrgStTree(@RequestParam Map<String, Object> map, HttpServletRequest request) {
        return commonService.getOrgStTree(map, request);
    }

    /**
     * 查询所有数据权限
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("selAuthData")
    public Result selAuthData(@RequestBody Map<String, Object> map) {
        return commonService.selAuthData(map);
    }

    /**
     * 查询系统图标
     * @return Result 返回数据
     */
    @GetMapping("selIcons")
    public Result selIcons(){
        return commonService.selIcons();
    }

    //获取码值
    @GetMapping("getCodeTypeData2")
    @SysLog(tableName = "param_code",action = Constant.ACTION_NAME.QUERY,menuName = "获取码值")
    public Result getCodeTypeData2(@RequestParam Map<String,Object> paramMap){
        try{
            List<Map<String,Object>> list = commonService.getCodeTypeData2(paramMap);

            return Result.ok(list);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }

    @RequestMapping(value = "/dict/code/{code}")
    public Result getDataDict(@PathVariable String code){
        return commonService.getDataDict(code);
    }

    //批量获取码值
    @PostMapping("getCodeTypeDataList")
    @SysLog(tableName = "param_code",action = Constant.ACTION_NAME.QUERY,menuName = "批量获取码值")
    public Result getCodeTypeDataList(@RequestBody List<String>  codeType){
        Map<String,Object> dataMap = commonService.getCodeTypeDataList(codeType);
        return Result.ok(dataMap);
    }

    //获取递归码值
    @GetMapping("getRecurveCode")
    @SysLog(tableName = "param_code",action = Constant.ACTION_NAME.QUERY,menuName = "获取递归码值")
    public Result getRecurveCode(@RequestParam Map<String,Object> paramMap){
        try{
            List<Map<String,Object>> list = commonService.getRecurveCode(paramMap);
            return Result.ok(list);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }

    /**
     * 提交附件
     * @author 廖昊源
     * @date 2023/3/21
     * @param multipartFile
     * @param seqno
     * @param userId
     * @param fileDatas
     * @return
     */
    @PostMapping("/uploadfile")
    public Result uploadfile( @RequestPart(value = "file",required = false) MultipartFile[] multipartFile,
                              @RequestParam(value = "seqno") String seqno,
                              @RequestParam(value = "userId") String userId,
                              @RequestParam(value = "fileDatas",required = false) String fileDatas,
                              @RequestParam(value = "delfiles",required = false) String delfiles){
        return commonService.uploadfile(multipartFile,seqno,userId,fileDatas,delfiles);
    }

    /**
     * 查看附件
     *      * @author 廖昊源
     *      * @date 2023/3/21
     * @param map
     * @return
     */
    @GetMapping("getFileList")
    public Result getFileList(@RequestParam Map<String,Object> map){
        return commonService.getFileList(map.get("seqno").toString());
    }

    /**
     * 提交审批时删除上传附件
     * @param fileDel
     * @return
     */
    @PostMapping("delFile")
    public Result delFile(@RequestBody FileDel fileDel) {
        return commonService.delFile(fileDel);
    }

    @GetMapping("selectIndex")
    public Result selectIndex(){
        List<Index> indices = commonService.selectIndex();
        return Result.ok(indices);
    }

}
